
package com.probiz.estore.customer.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;

import com.probiz.estore.common.model.customer.Feedback;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.customer.dao.FeedbackDao;
import com.probiz.estore.customer.model.FeedbackSearchCriteria;

/**
 * Feedback Data Access Object (DAO) implementation. Developer introduced
 * interfaces should be declared here. Won't get overwritten.
 */
public class FeedbackDaoImpl extends HibernateGenericDaoImpl<Feedback> implements FeedbackDao {
	
	protected void initFilterHqlMap() {
		filterHqlMap.put(Feedback.FilterHqlName.MY_FEEDBACK_LIST, "from Feedback f where f.customer.appuserId=? and f.parent is null order by f.createTime desc");
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.FeedbackDao#getCountsOfCustomer(java.lang.Integer)
	 */
	public Integer[] getCountsOfCustomer(Integer customerId){
		int myFeedbackListCount = this.countByFilter(Feedback.FilterHqlName.MY_FEEDBACK_LIST, customerId).intValue();
		Integer[] counts = new Integer[1];
		counts[0] = Integer.valueOf(myFeedbackListCount);
		return counts;
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.FeedbackDao#searchByCriteria(com.probiz.estore.customer.model.FeedbackSearchCriteria, com.probiz.estore.core.search.SearchCriteria)
	 */
	public List<Feedback> searchByCriteria(FeedbackSearchCriteria feedbackSearchCriteria, SearchCriteria searchCriteria){
		StringBuffer hqlsb = new StringBuffer(100);
	       List<Object> paramList = new ArrayList<Object>();
	       //start:构建HQL语句
	       //特别注意，因为user可为空，所以在查询评论人名称时候必须加上左外连接。
	       hqlsb.append("select f from Feedback f left join f.customer as u where 1=1 ");
	      
	       //-begin:*********回复的反馈id，该属性非常重要*********
	       if (null != feedbackSearchCriteria.getParentId()) {
	    	   hqlsb.append(" and  f.parent.feedbackId = ? ");
	    	   paramList.add(feedbackSearchCriteria.getParentId());
	       }else{
	    	   hqlsb.append(" and  f.parent is null ");
	       }
	       //-end:
	       //-begin:标题
	       if (StringUtils.isNotBlank(feedbackSearchCriteria.getSubject())) {
	           hqlsb.append(" and  f.subject like ? ");
	           paramList.add("%" + feedbackSearchCriteria.getSubject() + "%");
	       }
	       //-end:标题
	       //-begin:内容
	       if (StringUtils.isNotBlank(feedbackSearchCriteria.getMessage())) {
	    	   hqlsb.append(" and  f.message like ? ");
	    	   paramList.add("%" + feedbackSearchCriteria.getMessage() + "%");
	       }
	       //-end:内容
	       
	       //-start:顾客名称
	       if (StringUtils.isNotBlank(feedbackSearchCriteria.getFeedbackUserName())) {
	    	   hqlsb.append(" and ( (f.customerName like ?)");
	    	   hqlsb.append(" or (u is not null and u.firstname like ?)");
	    	   hqlsb.append(" or (u is not null and u.username like ?) )");
	    	   for (int i = 0; i < 3; i++) {
	    		   paramList.add("%" + feedbackSearchCriteria.getFeedbackUserName() + "%");
	    	   }	
	       }
	       //-end:顾客名称
	       //-start:状态:显示/隐藏
	       if (null != feedbackSearchCriteria.getStatus()) {
	           hqlsb.append(" and f.status = ?");
	           paramList.add(feedbackSearchCriteria.getStatus());
	       }
	       //-end:状态:显示/隐藏
	       //-start:状态:已读/未读
	       if (null != feedbackSearchCriteria.getIsRead()) {
	    	   hqlsb.append(" and f.isRead = ?");
	    	   paramList.add(feedbackSearchCriteria.getIsRead());
	       }
	       //-end:状态:已读/未读
	       //-start:状态:回复数量 已回复/等待回复
	       if(null != feedbackSearchCriteria.getIsAlreadyReply()){
	    	   if(feedbackSearchCriteria.getIsAlreadyReply().equals(Feedback.STATUS_REPLY)){
	    		   hqlsb.append(" and f.onShowReplyCount >= 1");
	    	   }else{
	    		   hqlsb.append(" and f.onShowReplyCount < 1");
	    	   }
	    	   
	       }
	       //-end:状态:回复数量 已回复/等待回复
	       
	     //start:order by
	       if (null != feedbackSearchCriteria.getOrderby()) {
	           hqlsb.append(" order by ").append(getSortByString(feedbackSearchCriteria.getOrderby()));
	       }else{
	           hqlsb.append(" order by f.createTime desc,f.feedbackId desc");
	       }
	       //end:order by
	       
	       
	       //end:构建HQL语句
	       searchCriteria.setHql(hqlsb.toString());
	       searchCriteria.removeParamValues();
	       for (Object object : paramList) {
	           searchCriteria.addParamValue(object);
	       }
	       List<Feedback> feedbackList =  searchByCriteria(searchCriteria);
	       return feedbackList;
	}
	
	/**
	    * 根据orderby获得 hql语句
	    * @param orderby 排序id
	    * @return hql语句
	    */
	   private String getSortByString(short orderby) {
	       String orderByString;
	       switch (orderby) {
	           case 3:
	               orderByString = "convert_gbk(f.message) asc";
	               break;
	           case 4:
	               orderByString = "convert_gbk(f.message) desc";
	               break;
	           case 9:
	        	   orderByString = "f.createTime asc";
	        	   break;
	           case 10:
	        	   orderByString = "f.createTime desc";
	        	   break;
	           default:
	               orderByString = "f.createTime desc,f.feedbackId desc";
	               break;
	       }
	       return orderByString;
	   }
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.FeedbackDao#getReplyCountByFeedbackId(java.lang.Integer)
	 */
	public Long getReplyCountByFeedbackId(Integer feedbackId){
		return this.countByHql("from Feedback f where f.parent.feedbackId = ?", feedbackId);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.FeedbackDao#getOnShowReplyCountByFeedbackId(java.lang.Integer)
	 */
	public Long getOnShowReplyCountByFeedbackId(Integer feedbackId){
		return this.countByHql("from Feedback f where f.parent.feedbackId = ? and f.status=1", feedbackId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.FeedbackDao#getOnShowReplyListByFeedbackId(java.lang.Integer)
	 */
	public List<Feedback> getOnShowReplyListByFeedbackId(Integer feedbackId){
		return this.findByHql("from Feedback f where f.parent.feedbackId =? and f.status = 1 order by f.createTime asc", feedbackId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.FeedbackDao#countFeedbackTotal()
	 */
	public Long countFeedbackTotal(){
		return this.countByHql("from Feedback f where f.parent is null");
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.FeedbackDao#countUnreplyFeedbackTotal()
	 */
	public Long countUnreplyFeedbackTotal(){
		return this.countByHql("from Feedback f where f.parent is null and f.onShowReplyCount <1");
	}
	
}